package vsvm.data.filter;

import java.util.ArrayList;
import vsvm.data.model.AbstractAttribute;
import vsvm.data.model.AbstractDataModel;
import vsvm.gui.Environment;
import vsvm.gui.forms.ConfigureNormalizeFilterDialog;
import vsvm.math.statistics.Characteristics;

public class NormalizeAttributeFilter extends AbstractAttributeFilter{
	private static final long serialVersionUID = 100L;
	private double min, max;
	private double minA, maxA;
	
	public NormalizeAttributeFilter() {
		dataModel = null;
		attributes = null;
		min = 0;
		max = 1;
		initializeStuff();
		
		initializeFilters();
	}
	
	public NormalizeAttributeFilter(AbstractDataModel model, ArrayList<AbstractAttribute> attr) {
		dataModel = model;
		attributes = attr;
		min = 0; max = 1;
		initializeStuff();
		
		initializeFilters();
	}
	
	public NormalizeAttributeFilter(AbstractDataModel model, ArrayList<AbstractAttribute> attr, double min, double max) {
		this(model, attr);
		this.min = min; this.max = max;
		initializeStuff();
		
		initializeFilters();
	}
	
	@Override
	protected double transform(double val) {
		if (Math.abs(maxA - minA) > 1e-8)
			return min + (val - minA)*(max - min)/(maxA - minA);
		
		return 0.5;
	}

	@Override
	public AbstractDataModel filter() {
		AbstractDataModel cln = dataModel.clone();
		updateDataModel(cln);
		return cln;
	}
	
	public void update() {
		updateDataModel(dataModel);
	}
	
	private void updateDataModel(AbstractDataModel dm) {
		for (int i = 0; i < attributes.size(); ++i) {
			double[] attrs = dm.getAttributeData(attributes.get(i));
			maxA = Characteristics.getMax(attrs);
			minA = Characteristics.getMin(attrs);
			filterDataModel(dm, attributes.get(i));		
		}
		
	}

	public void configure() {
		ConfigureNormalizeFilterDialog d = new ConfigureNormalizeFilterDialog(Environment.getEnvironment().getMainFrame(), this);
		d.setVisible(true);		
	}

	public void executeAction(String action) {
		// TODO Auto-generated method stub
		
	}

	public String[] getActions() {
		// TODO Auto-generated method stub
		return new String[0];
	}

	public double getMax() {
		return max;
	}

	public void setMax(double max) {
		this.max = max;
	}

	public double getMin() {
		return min;
	}

	public void setMin(double min) {
		this.min = min;
	}

	public String getQuickInfo() {
		StringBuffer list = new StringBuffer("Normalize to range [" + min + ";" + max + "]\n");
		return list.toString();
	}
	

}
